package com.si.sea_island.utils;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String timeAddDay(String timeStr, Integer dayNum) {
        return timeAddMinute(timeStr, dayNum*1440);
    }

    public static String timeAddMinute(String timeStr, Integer timeSolt) {
        try {
            Date nowDate = defaultFormat(timeStr);
            Long addTime = timeSolt*60*1000L;
            Date newDate = new Date(Long.valueOf(nowDate.getTime()).longValue() + addTime.longValue());
            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simple.format(newDate);
        } catch (ParseException e) {
//            e.printStackTrace();
            return null;
        }
    }

    //字符串转date
    public static Date defaultFormat(String dateStr) throws ParseException {
        if ((StringUtils.isBlank(dateStr))) {
            return null;
        }
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simple.parse(dateStr);
    }

    public static String secondToHour(Integer date) {
        if (date<60) {
            return date+"秒";
        }else if (date>60&&date<3600) {
            int m = date/60;
            int s = date%60;
            return m+"分"+s+"秒";
        }else {
            int h = date/3600;
            int m = (date%3600)/60;
            int s = (date%3600)%60;
            return h+"小时"+m+"分"+s+"秒";
        }

    }

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

    //获取前一天
    public static String getLastDay(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date=null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day=calendar.get(Calendar.DATE);
        //此处修改为+1则是获取后一天
        calendar.set(Calendar.DATE,day-1);
        String lastDay = sdf.format(calendar.getTime());
        return lastDay;
    }

    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String getExpirationTime(String ageing) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Long time = date.getTime();
        Long addTime = Long.valueOf(String.valueOf(Integer.parseInt(ageing))).longValue()
                * Long.valueOf(24*60*60*1000).longValue();
        time = time.longValue()+addTime.longValue();
        return sdf.format(new Date(time));
    }

    public static String getNowMillTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        return sdf.format(new Date());
    }

    public static String getNowDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    public static String getNowMouth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(new Date());
    }

    public static String addDay(Date date,long day) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
        time+=day; // 相加得到新的毫秒数
        return sdf.format(new Date(time)); // 将毫秒数转换成日期
    }

    public static String minusDay(Date date,long day) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
        time-=day; // 相加得到新的毫秒数
        return sdf.format(new Date(time)); // 将毫秒数转换成日期
    }

    public static String getCreateOrderTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    /**
     * String -> Date
     * @param strDate
     * @param format "yyyy-MM-dd"
     * @return
     */
    public static Date parse(String strDate, String format) {
        if (strDate == null)
            return new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
//            e.printStackTrace();
            System.out.println("错误 == DateUtils.parse  =="+strDate+"=="+format);
            return new Date();
        }
    }

    /**
     * 根据date获取年龄
     * @param birthDay
     * @return
     */
    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) { //出生日期晚于当前时间，无法计算
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前，年龄减一
            } else {
                age--;//当前月份在生日之前，年龄减一1
            }
        } return age;
    }

    /**
     * 相差月份
     * @param date1
     * @param date2
     */
    public static int getMonthNum(Date date1,Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(date1);
        c2.setTime(date2);

        int surplus = c2.get(Calendar.DATE) - c1.get(Calendar.DATE);
        int result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        int month = (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12;
//        System.out.println("天数差:"+surplus);
//        surplus = surplus > 0 ? 1 : 0;
        surplus = 0;
//        System.out.println("天数差:"+surplus+"--相差月份：" + (Math.abs(month + result) + surplus));

        return Math.abs(month + result) + surplus;
    }

    /**
     * 相差年份
     * @param date1
     * @param date2
     */
    public static int getYearNum(Date date1,Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(date1);
        c2.setTime(date2);

        int result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
//        System.out.println("日期："+date1+"|"+date2+"|,相差"+Math.abs(result)+"年");
        return result == 0 ? 1 : Math.abs(result);
    }

    /**
     * 判断当前时间是否大于某个时点
     * @param time HH:mm:ss
     * @return
     */
    public static boolean isAfterTime(String time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime localTime=LocalTime.parse(time,dtf);
        return LocalTime.now().isAfter(localTime);
    }

    /**
     * 判断当前时间是否小于某个时点
     * @param time HH:mm:ss
     * @return
     */
    public static boolean isBeforeTime(String time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime localTime=LocalTime.parse(time,dtf);
        return LocalTime.now().isBefore(localTime);
    }

    public static Boolean getDiffDay(String createTime, String nowTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse1 = sdf.parse(DateUtils.getNowTime());
        Date date1 = sdf.parse(createTime);
        Date date2= new Date(parse1.getTime() + 15 * 24 * 60 * 60 * 1000 + 15 * 24 * 60 * 60 * 1000);
        if (date1.before(date2)){
            return false;
        }else {
            return true;
        }
    }


    public static String reduce(String expirationTime, String lastEffectiveTime) {
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = sdf.parse(expirationTime);
            Date date2 = sdf.parse(lastEffectiveTime);
            long time = date1.getTime() - date2.getTime();
            return String.valueOf(time);
        }catch (Exception e){
            return "0";
        }
    }

}
